﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Trangtrai.Controller.Global;
using System.Data.SqlClient;
using System.Data;
namespace Trangtrai.Controller.Authorized
{
    public class LoginController
    {
        public int checkLoginStatus(String usercode, String password)
        {
            int roleID = -1;
            if (ConnectionClass.ConnectToDB())
            {
                
                SqlDataAdapter userAdapter = new SqlDataAdapter("Select ROLE_ID FROM [USER] WHERE USER_CODE = '" + usercode
                    + "' AND PASSWORD = '" + password+"'", ConnectionClass.sqlConn);


                DataSet user = new DataSet();
                userAdapter.Fill(user, "USER");
                if (user.Tables["USER"].Rows.Count == 0)
                {
                    return -1;
                }
                else
                {
                    foreach (DataRow pRow in user.Tables["USER"].Rows)
                    {
                        roleID = Int32.Parse(pRow["ROLE_ID"].ToString());
                    }
                    //SqlDataAdapter roleAdapter = new SqlDataAdapter("Select ROLE_NAME FROM [ROLE] WHERE ROLE_ID = " + roleID, ConnectionClass.sqlConn);
                    ConnectionClass.closeConn();
                    return roleID;
                }
            }
            else
            {
                ConnectionClass.closeConn();
                return -1;
            }
        }
    }
}